//
// Created by Crestimes on 24-5-10.
//

#include "Printer.h"

#include <iostream>
#include <queue>
#include <utility>

Printer::Printer(Parser p): parser(std::move(p)) {}

void Printer::print() {
    TreeNode* root = parser.getSyntaxTree();
    std::queue<TreeNode*> qu;
    qu.push(root);
    int lastCount = 1;
    int thisCount = 0;
    while (!qu.empty()) {
        while (lastCount--) {
            auto front = qu.front();
            qu.pop();
            if (!front->leaf_str.empty()) {
                std::cout << front->leaf_str << " ";
            }
            else {
                std::cout << branchType2str[front->branch_type] << " ";
            }
            for (auto child : front->children) {
                qu.push(child);
                thisCount++;
            }
        }
        std::cout << std::endl;
        lastCount = thisCount;
        thisCount = 0;
    }
}
